<?php
    class category_Ajax extends Ajax
    {
        public function getCategorySuggetsion()
        {
            if(!caching::hasCache('aCategories'))
            {
                $aMenus = caching::createCacheMenu();       
            }
            else
            {
                $aMenus = caching::getCache('aCategories');
            }

            core::checkSession();
            $oDatabase = new database();
            $sUserId = end(explode('/',$_SESSION['user_uri']));
            
            $sQuery = "SELECT * FROM user_suggestion WHERE type = 'category' AND user_id = '".$sUserId."'";
            $aListFilters = $oDatabase->getListObject($sQuery,false,true);
            foreach($aListFilters as $filter)
            {
                unset($aMenus['sub'][$filter['filter_id']]);
            }
            
            $i = 0;
            $iMax = 10;
            foreach($aMenus['sub'] as $aCategory)
            {
                $i++;
                echo '<div class="suggetion_category_item" val="'.$aCategory['uri'].'">';
                echo '<div class="image_content">';
                echo '<a class="no_link" href="javascript:void(0);"><img src="'.URL::getLink('upload/'.$aCategory['image']).'"></a>';
                echo '</div>';
                echo '<div class="info_content">';
                echo '<p class="category_name">'.$aCategory['name'].'</p>';
                echo '</div>';
                echo '</div>';
                if($i == $iMax)
                {
                    return;
                }
            }
        }

        public function addCategorySuggestion()
        {
            core::checkSession();
            if(!database::isUser())
            {
                return;
            }
            $sUserId = end(explode('/',$_SESSION['user_uri']));
            $oDatabase = new database();
            $sQuery = "SELECT * FROM user_suggestion WHERE user_id = '".$sUserId."' AND filter_id = '".$_POST['categoryUri']."'";
            $result = $oDatabase->getListObject($sQuery);
            if(count($result) > 0)
            {
                echo 'OK';
                return;
            }
            $aData = array(
                'user_id' => $sUserId,
                'type' => 'category',
                'filter_id' => $_POST['categoryUri'],
                'filter' => '?categoryUri = "'.$_POST['categoryUri'].'"',
                'title' => $_POST['categoryName'],
            );
            $result = $oDatabase->add('user_suggestion',$aData);
            if($result)
            {
                echo 'OK';
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }

        public function addMenufacturerSuggestion()
        {
            core::checkSession();
            if(!database::isUser())
            {
                return;
            }
            $sUserId = end(explode('/',$_SESSION['user_uri']));
            $oDatabase = new database();
            $sQuery = "SELECT * FROM user_suggestion WHERE user_id = '".$sUserId."' AND filter_id = '".$_POST['menufacturerUri']."'";
            $result = $oDatabase->getListObject($sQuery);
            if(count($result) > 0)
            {
                echo 'OK';
                return;
            }
            $aData = array(
                'user_id' => $sUserId,
                'type' => 'menufacturer',
                'filter_id' => $_POST['menufacturerUri'],
                'filter' => '?menufacturerUri = "'.$_POST['menufacturerUri'].'"',
                'title' => $_POST['menufacturerName'],
            );
            $result = $oDatabase->add('user_suggestion',$aData);
            if($result)
            {
                echo 'OK';
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }

        public function loadMenufacturerSuggestion()
        {
            core::checkSession();
            $oDatabase = new database();
            $sUserId = end(explode('/',$_SESSION['user_uri']));
            $sQuery = "SELECT * FROM user_suggestion WHERE type = 'category' AND user_id = '".$sUserId."'";
            $aListFilters = $oDatabase->getListObject($sQuery,false,true);
            $sConditionFilter = '';
            if(count($aListFilters) > 0)
            {
                $sConditionFilter = 'FILTER(';
                foreach($aListFilters as $aCondition)
                {
                    $sConditionFilter = $sConditionFilter.$aCondition['filter'].' || ';
                }
                $sConditionFilter = trim($sConditionFilter," || ");
                $sConditionFilter = $sConditionFilter.').';
            }

            // Get menufacturer filter
            $sQuery = "SELECT * FROM user_suggestion WHERE type = 'menufacturer' AND user_id = '".$sUserId."'";
            $aListFilters = $oDatabase->getListObject($sQuery,false,true);
            $sMenufacturerConditionFilter = '';
            if(count($aListFilters) > 0)
            {
                $sMenufacturerConditionFilter = 'FILTER(';
                foreach($aListFilters as $aCondition)
                {
                    $sMenufacturerConditionFilter = $sMenufacturerConditionFilter.'?uri != "'.$aCondition['filter_id'].'" && ';
                }
                $sMenufacturerConditionFilter = trim($sMenufacturerConditionFilter," && ");
                $sMenufacturerConditionFilter = $sMenufacturerConditionFilter.').';
            }

            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *  
            WHERE {
            ?uri rdf:type bus:manufacturer.
            ?uri bus:name ?name.
            ?uri bus:hasCategory ?categoryUri."
            .$sConditionFilter.
            $sMenufacturerConditionFilter.
            "}
            LIMIT 6 
            ";

            $aResult = $oDatabase->loadQuery($sQuery);
            if(!empty($aResult['result']['rows']) && count($aResult['result']['rows']) > 0)
            {
                $aMenufacturers = $aResult['result']['rows'];
                foreach($aMenufacturers as $aMenufacturer)
                {
                    echo '<li val="'.$aMenufacturer['uri'].'">'.$aMenufacturer['name'].'</li>';
                }
            }
        }
    }
?>
